package com.hrm.dao.Impl;

import java.util.List;

import com.hrm.dao.DeptDao;
import com.hrm.domain.Dept;
import com.hrm.utils.JDBCTools;

/*
 * 部门数据处理层实现类
 */
@SuppressWarnings("all") // 使用注解抑制警告信息
public class DeptDaoImpl implements DeptDao {

	// 定义全局变量
	private Class dClass = Dept.class;

	// 获取全部部门信息
	@Override
	public List<Dept> getAllDept() {
		String sql = "select `ID` deptID, `NAME` deptName, `REMARK` deptRemark from `dept_inf`;";
		return JDBCTools.dataQuery(dClass, sql);
	}

	// 分页查询 + 模糊
	@Override
	public List<Dept> getDeptsByTerm(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select `ID` deptID, `NAME` deptName, `REMARK` deptRemark from `dept_inf` limit ?,?;";
		} else {// 输入了查询条件
			sql = "select `ID` deptID, `NAME` deptName, `REMARK` deptRemark from `dept_inf` where NAME like ? limit ?,?;";
		}
		return JDBCTools.dataQuery(dClass, sql, parametes);
	}

	// 获取查询结果的记录条数
	@Override
	public Integer getCount(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select count(*) from dept_inf;";
		} else { // 输入了查询条件
			sql = "select count(*) from dept_inf where NAME like ?;";
		}
		return JDBCTools.dataCount(sql, parametes);
	}

	// 添加部门
	@Override
	public Integer addDept(Dept dept) {
		String sql = "INSERT INTO `dept_inf` (`NAME`, `REMARK`) VALUES (?, ?);";
		return JDBCTools.dataUpdate(sql, dept.getDeptName(), dept.getDeptRemark());
	}

	// 根据部门名称查找部门
	@Override
	public Dept getDeptByDN(String deptName) {
		String sql = "select `ID` deptID, `NAME` deptName, `REMARK` deptRemark from `dept_inf` where `NAME` = ?;";
		Dept dept = null;
		List<Dept> depts = JDBCTools.dataQuery(dClass, sql, deptName);
		if (!depts.isEmpty()) {
			dept = depts.get(0);
		}
		return dept;
	}

	// 根据部门ID获取指定部门
	@Override
	public Dept getDeptByID(int ID) {
		String sql = "select `ID` deptID, `NAME` deptName, `REMARK` deptRemark from `dept_inf` where `ID` = ?;";
		Dept dept = null;
		List<Dept> depts = JDBCTools.dataQuery(dClass, sql, ID);
		if (!depts.isEmpty()) {
			dept = depts.get(0);
		}
		return dept;
	}

	// 根据部门ID删除指定部门
	@Override
	public Integer delDept(int ID) {
		String sql = "delete from `dept_inf` where `ID` = ?;";
		return JDBCTools.dataUpdate(sql, ID);
	}

	// 根据部门ID修改指定的部门信息
	@Override
	public Integer modifyDeptByID(Dept newDept, int ID) {
		String sql = "UPDATE `dept_inf` SET `NAME` = ?, `REMARK` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, newDept.getDeptName(), newDept.getDeptRemark(), ID);
	}

}
